var data = [];
var xCoordoor = [];
var id = location.href;
var arr = id.split("?");

if(arr.length == 2) {
	var arr1 = arr[1].split("&&");
	$("#name").text(arr1[0]);
	var oldtime;
	var newtime = arr1[1];
	var arr2 = newtime.split(":");
	oldtime = arr2[0] + ":00:00";
	$("#currentTime").attr("value", arr1[1]);
	$("#historyTime").attr("value", oldtime);
}

//字符时间转标准时间
function convertDataTime(theDateTime){
	theDateTime=insert_flg(theDateTime,"-",4);
	theDateTime=insert_flg(theDateTime,"-",7);
	theDateTime=insert_flg(theDateTime," ",10);
	theDateTime=insert_flg(theDateTime,":",13);
	theDateTime=insert_flg(theDateTime,":",16);
	theDateTime=new Date(Date.parse(new Date(theDateTime))-8*60*60*1000);
	
	var month = theDateTime.getMonth()+1;
	var strDate = theDateTime.getDate();
	if(month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if(strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	
	var hours = theDateTime.getHours();
	if(hours >= 0 && hours <= 9) {
		hours = "0" + hours;
	}
	var currentdate = theDateTime.getFullYear() + month + strDate +
		hours + "0000";
	return currentdate;
}

function insert_flg(str,flg,sn){
    var newstr="";
        var tmp=str.substring(0, sn);
        var next=str.substring(sn, str.length-1);
        newstr+=tmp+flg;
newstr+=next;
    return newstr;
}

//历史数据查询展示
function query() {
	var historyValue = document.getElementById("start_time").value;
	historyValue = historyValue.replace(":", "");
	historyValue = historyValue.replace("-", "");
	historyValue = historyValue.replace("-", "");
	historyValue = historyValue.replace(" ", "");
	historyValue = parseFloat(historyValue.substr(0, historyValue.length - 5));
	historyValue = historyValue.toString() + "0000";
	var currentValue = document.getElementById("end_time").value;
	currentValue = currentValue.replace(":", "");
	currentValue = currentValue.replace("-", "");
	currentValue = currentValue.replace("-", "");
	currentValue = currentValue.replace(" ", "");
	currentValue = parseFloat(currentValue.substr(0, currentValue.length - 5));
	currentValue = currentValue.toString() + "0000";
	historyValue=convertDataTime(historyValue);
		currentValue=convertDataTime(currentValue);
	var code = document.getElementById("name").innerText;
	var url = "http://61.157.118.158:8088/sndm/api/surfChnMulHor/getSurfEleByTimeRangeAndStaID?dataCode=SURF_CHN_MUL_HOR&elements=Station_Name,City,Datetime,Station_Id_C,Station_levl,PRS,TEM,RHU,PRE_1h,WIN_D_INST,WIN_S_INST&timeRange=[" + historyValue + "," + currentValue + "]&staIds=" + code;
	$.ajax({
		type: 'post',
		url: url,
		data: $("#myform").serialize(),
		cache: false,
		dataType: 'json',
		success: function(data) {
			var historydata=data;
			var tem = {
				"name": "温度",
				"yAxis": 0,
				"data": []
			}
			var humidity = {
				"name": "湿度",
				"yAxis": 1,
				"data": []
			}
			var WindSpeed = {
				"name": "风速",
				"data": []
			}

			for(var i = 0; i < historydata.rowCount; i++) {
				xCoordoor.push(historydata.DS[i].Datetime);
				var tem1=parseFloat(historydata.DS[i].TEM);
				var rhu=parseFloat(historydata.DS[i].RHU);
				var winsinst=parseFloat(historydata.DS[i].WIN_S_INST);
				if(tem1==999999){
					tem1=0;
				}
				if(rhu==999999){
					rhu=0;
				}
				if(winsinst==999999){
					winsinst=0;
				}
				tem.data.push(tem1);
				humidity.data.push(rhu);
				WindSpeed.data.push(winsinst);
				//debugger;
			}
			data = [
				tem,
				humidity,
				WindSpeed
			];
			chart = Highcharts.chart('container', {
				char: {
					type: 'line'
				},
				title: {
					text: "站点详情"
				},
				subtitle: {
					text: ''
				},
				xAxis: {
					categories: xCoordoor
				},
				yAxis: [

					{ // Primary yAxis
						labels: {
							format: '{value}°C',
							style: {
								color: Highcharts.getOptions().colors[2]
							}
						},
						title: {
							text: '温度',
							style: {
								color: Highcharts.getOptions().colors[2]
							}
						}
					}, { // Secondary yAxis
						gridLineWidth: 0,
						title: {
							text: '湿度',
							style: {
								color: Highcharts.getOptions().colors[0]
							}
						},
						labels: {
							format: '{value} mm',
							style: {
								color: Highcharts.getOptions().colors[0]
							}
						},
						opposite: true
					}
				],
				legend: {
					align: 'left',
					verticalAlign: 'top',
					y: 10,
					floating: true,
					borderWidth: 0
				},
				series: data
			})
		}
	});

}

chart = Highcharts.chart('container', {
	char: {
		type: 'line'
		
	},
	title: {
		text: "站点详情"
	},
	subtitle: {
		text: ''
	},
	xAxis: {
		categories: xCoordoor
	},
	yAxis: [

		{ // Primary yAxis
			labels: {
				format: '{value}°C',
				style: {
					color: Highcharts.getOptions().colors[2]
				}
			},
			title: {
				text: '温度',
				style: {
					color: Highcharts.getOptions().colors[2]
				}
			}
		}, { // Secondary yAxis
			gridLineWidth: 0,
			title: {
				text: '湿度',
				style: {
					color: Highcharts.getOptions().colors[0]
				}
			},
			labels: {
				format: '{value} mm',
				style: {
					color: Highcharts.getOptions().colors[0]
				}
			},
			opposite: true
		}
	],
	legend: {
		align: 'left',
		verticalAlign: 'top',
		y: 10,
		floating: true,
		borderWidth: 0
	},
	series: data
})